using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NorthwindWeb.Models;

namespace NorthwindWeb.Controllers
{   
    public class TerritoryController : Controller
    {
		private readonly IRegionRepository regionRepository;
		private readonly ITerritoryRepository territoryRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public TerritoryController() : this(new RegionRepository(), new TerritoryRepository())
        {
        }

        public TerritoryController(IRegionRepository regionRepository, ITerritoryRepository territoryRepository)
        {
			this.regionRepository = regionRepository;
			this.territoryRepository = territoryRepository;
        }

        //
        // GET: /Territory/

        public ViewResult Index()
        {
            ViewBag.PossibleRegions = regionRepository.All;
            return View(territoryRepository.AllIncluding(territory => territory.Region, territory => territory.Employees));
        }

        //
        // POST: /Territory/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "TerritoryID")]Territory territory)
        {
            if (ModelState.IsValid) {
                territoryRepository.InsertOrUpdate(territory);
                territoryRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleRegions = regionRepository.All;
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /Territory/Edit/5

        [HttpPost]
        public ActionResult Edit(Territory territory)
        {
            if (ModelState.IsValid) {
                territoryRepository.InsertOrUpdate(territory);
                territoryRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleRegions = regionRepository.All;
                return RedirectToAction("Index");
			}
        }

        //
        // POST: /Territory/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            territoryRepository.Delete(id);
            territoryRepository.Save();

            return RedirectToAction("Index");
        }

        
        public JsonResult territoryGridData(int rows, int page)
        {
        	var count = territoryRepository.All.Count();
            var pageData = territoryRepository.All.OrderBy(x => x.TerritoryID).Skip((page - 1) * rows).Take(rows).Select(o => new {TerritoryID = o.TerritoryID, TerritoryDescription = o.TerritoryDescription, RegionID = o.RegionID});
            return Json(new { 
            page = page,
            records = count,
            rows = pageData,
            total = Math.Ceiling((decimal)count / rows)
            }, JsonRequestBehavior.AllowGet);
        }
        
        
    }
}

